package com.asdan.seededu.web.controller;




import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asdan.seededu.entity.security.Student;
import com.asdan.seededu.service.StudentService;
import com.asdan.seededu.service.impl.StudentServiceImpl;
import com.asdan.seededu.util.Pager;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-user")
@RequestMapping("student")
public class StudentController {
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addStudent(String name,String schoolId ,String grade ,String email ,String cellPhone ,Model model) {
		System.out.println(name + " "+email +" "
				+cellPhone+"  "+grade + " " +schoolId) ;
		StudentService ss = new StudentServiceImpl() ;
		if(ss.getStudentByEmailOrID(email, -1)!=null){
			model.addAttribute("message", "增加失败，邮箱已注册！") ;
		}else{
		ss = new StudentServiceImpl() ;
		Student stu = new Student(name, Integer.parseInt(schoolId) , grade , email , cellPhone ) ;
		Boolean isadd = ss.addStudent(stu) ;
		if(isadd == true ){
			model.addAttribute("message", "增加成功！") ;
		}else{
			model.addAttribute("message", "增加失败，请重新输入！") ;
		}
		}
		return showStuList("","" ,"" ,"" ,"" ,"" ,model);
	}
	
	@RequestMapping(value="/showStuList",method=RequestMethod.GET)
	public String showStuList(String name,String schoolId ,String grade ,String cellPhone ,String subProId,String pageNo,Model model ) {
		StudentService ss = new StudentServiceImpl() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		Pager<Student> page = ss.getStudents(name, schoolId, grade, cellPhone, subProId, Integer.parseInt(pageNo));
		
		model.addAttribute("list", page.getDatas());
		model.addAttribute("pageNo",page.getOffset());
		model.addAttribute("pageCount", page.getTotalPage());
		model.addAttribute("totalCount",page.getTotal());
		model.addAttribute("size", page.getSize()) ;
		return "users";
	}
	
	@RequestMapping(value="/{suid}/delStu",method=RequestMethod.GET)
	public String delStu(@PathVariable String suid ,Model model) {
		System.out.println(11111);
		StudentService ss = new StudentServiceImpl() ;
		boolean bool = ss.deleteStudent(suid) ;
		System.out.println(bool);
		if(bool){
			model.addAttribute("message" , "删除信息成功！") ;
		}else{
			model.addAttribute("message" , "删除信息失败！") ;
		}
		return this.showStuList("", "", "", "", "", "", model) ;
	}
	
	
	@RequestMapping(value="/{suid}/detail",method=RequestMethod.GET)
	public String getStuByID(@PathVariable String suid,Model model){
		StudentService ss = new StudentServiceImpl() ;
		Student stu = ss.getStudentByEmailOrID(null, Integer.parseInt(suid)) ;
		model.addAttribute("stu", stu) ;
		return "userdetail" ;
	}
	
	@RequestMapping(value="/{suid}/reviseusers",method=RequestMethod.GET)
	public String updateStuByIDreviseusers(@PathVariable String suid,Model model){
		StudentService ss = new StudentServiceImpl() ;
		Student stu = ss.getStudentByEmailOrID(null, Integer.parseInt(suid)) ;
		model.addAttribute("stu", stu) ;
		return "reviseusers" ;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Student stu ,Model model){
		System.out.println(stu.getName());
		System.out.println(stu.getSex());
		StudentService ss = new StudentServiceImpl() ;
		boolean temp = ss.updateStudent(stu) ;
		model.addAttribute("stu",new StudentServiceImpl().getStudentByEmailOrID("", stu.getSuid())) ;
		if(temp){
			model.addAttribute("message", "更改成功！") ;
		}else{
			model.addAttribute("message", "更改失败") ;
		}
		return "userdetail" ;
	}
	@RequestMapping(value="/showStuBypage",method=RequestMethod.POST,produces="test/html;charset=UTF-8")
	@ResponseBody
	public String showStuBypage(String name,String schoolId ,String grade ,String cellPhone ,String subProId,String pageNo,Model model){
		System.out.println(name);
		StudentService ss = new StudentServiceImpl() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		Pager<Student> page = ss.getStudents(name, schoolId, grade, cellPhone, subProId, Integer.parseInt(pageNo));
		System.out.println(JsonUtils.toJsonString(page));
		return JsonUtils.toJsonString(page) ;
	}	
}
